/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.class_finder;

import picounit.features.classloader.PackageUtil;
import picounit.util.Equal;
import picounit.verify.constraint.EqualUtil;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class FileSystem implements Equal<FileSystem> {
	private static final String CLASS_EXTENSION = ".class";
	private static final PackageUtil packageUtil = new PackageUtil();

	private final Map<File, File[]> cache = new HashMap<File, File[]>();

	public File directoryContaining(Class<?> aClass) {
		return classFile(aClass).getParentFile();
	}

	public File sourceRoot(Class<?> aClass) {
		File file = classFile(aClass);

		for (int sourceDepth = packageUtil.sourceDepth(aClass); sourceDepth > 0; sourceDepth--) {
			file = file.getParentFile();
		}

		return file;
	}

	public File[] filesIn(File directory) {
		File[] files = cache.get(directory);
	
		if (files == null) {
			files = directory.listFiles();
			if (files == null) {
				files = new File[0];
			}
	
			cache.put(directory, files);
		}
	
		return files;
	}

	public String className(File classFile, File sourceRoot) {
		String relativeClassFile =
			classFile.getAbsolutePath().substring(sourceRoot.getAbsolutePath().length() + 1);

		return relativeClassFile.substring(0, relativeClassFile.length() - CLASS_EXTENSION.length())
			.replace(File.separatorChar, '.');
	}

	public boolean isClass(File file) {
		return file.isFile() && file.getAbsolutePath().endsWith(CLASS_EXTENSION);
	}

	@SuppressWarnings("deprecation")
	public File classFile(Class<?> aClass) {
		return new File(URLDecoder.decode(classURL(aClass).getFile()));
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Equal methods
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	public boolean equalTo(FileSystem other) {
		return this == other;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Object
	///////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public boolean equals(Object object) {
		return EqualUtil.isEqual(this, object);
	}

	@Override
	public int hashCode() {
		return getClass().hashCode();
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Implementation
	///////////////////////////////////////////////////////////////////////////////////////////////

	private URL classURL(Class<?> aClass) {
		return aClass.getResource('/' + relativeClassFileName(aClass));
	}

	private String relativeClassFileName(Class<?> aClass) {
		return aClass.getName().replace('.', '/') + ".class";
	}
}
